package com.fita.wechat.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.fita.entity.coupon.Coupon;
import com.fita.entity.coupon.CouponStatus;
import com.fita.entity.order.Order;
import com.fita.wechat.dao.CouponDao;
import com.fita.wechat.service.CouponService;
import com.fita.wechat.service.OrderService;

/**
 * 
 * @author dan
 *
 */
@Service("couponService")
public class CouponServiceImpl implements CouponService{
	@Autowired
	private CouponService couponService;

	@Dao
    private CouponDao couponDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Autowired
    private OrderService orderService;

	/* (non-Javadoc)
	 * @see com.fita.wechat.service.CouponService#listAllAvailableCoupons(java.lang.Long, com.fita.entity.coupon.CouponStatus)
	 */
	public List<Coupon> listAllAvailableCoupons(Long userId,BigDecimal cnyPayPrice) {

		int cny = cnyPayPrice.intValue();
		
		return couponDao.listUserAllAvailableCoupons(userId,cny*100);
	}
	

	/* (non-Javadoc)
	 * @see com.fita.wechat.service.CouponService#listAllCoupons(java.lang.Long)
	 */
	@Override
	public List<Coupon> listAllCoupons(Long userId) {
		
		return couponDao.listUserAllCoupons(userId);
	}


	/* (non-Javadoc)
	 * @see com.fita.wechat.service.CouponService#load(java.lang.Long)
	 */
	@Override
	public Coupon load(Long couponId) {
		return genericDaoSupport.load(Coupon.class, couponId);
	}


	public void unBindCoupon(Long orderId){

		Order order = orderService.load(orderId);

		if(order.getUserCouponId() != null){

			Coupon coupon = couponService.load(order.getUserCouponId());

			coupon.setOrder(null);
			coupon.setStatus(CouponStatus.CLAIMED);
			order.setUserCouponId(null);
			genericDaoSupport.update(coupon);
			genericDaoSupport.update(order);

		}

	}
	/* (non-Javadoc)
	 * @see com.fita.wechat.service.CouponService#useCoupon(java.lang.Long, java.lang.Long)
	 */
	public Coupon bindCoupon(Long orderId, Long couponId) {
		
		//载入已绑定过的优惠券

		Coupon oldCoupon = this.load(couponId);

		if(oldCoupon.getOrder() != null ){
			//把订单绑定的优惠券解绑
			Order oldOrder = orderService.load(oldCoupon.getOrder().getId());
			oldOrder.setUserCouponId(null);
			genericDaoSupport.update(oldOrder);

		}

		//待绑定优惠券的订单
		Order newOrder = orderService.load(orderId);

		//先清除掉本订单原来绑定的优惠券
		if(newOrder.getUserCouponId() != null){

			Coupon coupon = this.load(newOrder.getUserCouponId());

			coupon.setOrder(null);
			coupon.setStatus(CouponStatus.CLAIMED);

			genericDaoSupport.update(coupon);
		}

		oldCoupon.setStatus(CouponStatus.OCCUPIED);
		oldCoupon.setOrder(newOrder);
		
		genericDaoSupport.update(oldCoupon);

		newOrder.setUserCouponId(oldCoupon.getId());
		
		genericDaoSupport.update(newOrder);
		
		return oldCoupon;
	}

	/* (non-Javadoc)
	 * @see com.fita.wechat.service.CouponService#useCoupon(com.fita.entity.order.Order)
	 */
	public void useCoupon(Order order) {
	    
	    if(order.getUserCouponId() != null) {
	        Coupon coupon = genericDaoSupport.load(Coupon.class, order.getUserCouponId());
	        
	        coupon.use();
	        
	        genericDaoSupport.update(coupon);
	    }
	}

	/* (non-Javadoc)
	 * @see com.fita.wechat.service.CouponService#resetCoupon(com.fita.entity.order.Order)
	 */
	public void resetCoupon(Order order) {
		
		List<Coupon> coupons = couponDao.listCouponsByOrder(order);
    	
    	
    	if(CollectionUtils.isNotEmpty(coupons)) {
    		
    		for (Coupon coupon :coupons ) {
    			
    			coupon.setOrder(null);
    			coupon.setStatus(CouponStatus.CLAIMED);
    			
    			genericDaoSupport.update(coupon);
    		}    		
    	} 		
		
		
	}


	/* (non-Javadoc)
	 * @see com.fita.wechat.service.CouponService#countAvailableCoupons(java.lang.Long)
	 */
	public int countAvailableCoupons(Long userId) {
		
		Map<String, Object> parameters = new HashMap<>();
		
		parameters.put("userId",userId);	
		parameters.put("status", CouponStatus.CLAIMED);
		
		return genericDaoSupport.count("SELECT * FROM Coupon WHERE  userId = :userId AND status = :status", parameters);
	}
    
	
	
	
}
